package com.woniu.tms.exam.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.Course;
import com.woniu.tms.entity.Exam;
import com.woniu.tms.entity.Role;
import com.woniu.tms.entity.Room;
import com.woniu.tms.entity.Score;
import com.woniu.tms.entity.ScoreExample;
import com.woniu.tms.entity.User;

import com.woniu.tms.exam.service.ExamService;
import com.woniu.tms.mapper.ScoreMapper;


/**
 * @author Leo
 *
 */

@Controller
@RequestMapping("score")
public class ScoreController {
@Autowired
ExamService examService;



/**
 * @return
 * 学生scoreindex
 */
@RequestMapping("student/index")
public String studentIndex() {
	return "WEB-INF/exam/score/studentScore";
}
/**
 * @param request
 * @return
 * 学生score页面取值
 */
@RequestMapping("student/indexDo")
@ResponseBody
public Map<String,Object> indexDo(HttpServletRequest request,@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
	Map<String,Object> map=new HashMap<String, Object>();
//	HttpSession session=request.getSession();
//	User loginUser=(User)session.getAttribute("loginUser");
	System.out.println("**********************************");
	System.out.println("ScoreController.indexDo()123");
//	System.out.println(loginUser.getUname());
	System.out.println("**********************************");

	
	try {
		List<Score> scoreList=examService.findScoreByStudentId(1);	
	
		//饼图	
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {

				count1++;
			}
			if (score.getScore() > 80) {
				count2++;
			}
			else{
				count3++;
			}
		}

		List<User> userList=examService.findUserAll();
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		
//		map.put("loginUser", loginUser);
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		map.put("userList",userList);
		map.put("examList",examList);
		map.put("courseList", courseList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	

	return map;
}



/**
 * @return
 * 教师index
 */
@RequestMapping("teacher/index")
public String teacherIndex() {
	return "WEB-INF/exam/score/teacherScore";
}
/**
 * @param request
 * @return
 * 教室score页面取值
 */
@RequestMapping("teacher/indexDo")
@ResponseBody
public Map<String,Object> teahcerIndexDo(HttpServletRequest request,@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
	Map<String,Object> map=new HashMap<String, Object>();
//	HttpSession session=request.getSession();
//	User loginUser=(User)session.getAttribute("loginUser");

	
	try {
		List<Integer> cidList=examService.findCidByTeacherId(2);
		List<Integer> studentIdList=examService.findUidByClassIdList(cidList);
		List<Score> scoreList=examService.findScoreByStudentIdList(studentIdList);
	
		//饼图	
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {

				count1++;
			}
			if (score.getScore() > 80) {
				count2++;
			}
			else{
				count3++;
			}
		}

		List<User> userList=examService.findUserAll();
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		
//		map.put("loginUser", loginUser);
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		map.put("userList",userList);
		map.put("examList",examList);
		map.put("courseList", courseList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	

	return map;
}

/**
 * @return
 * 跳转班主任score页面
 */
@RequestMapping("classmanager/index")
public String classmanagerIndex() {
	return "WEB-INF/exam/score/classmanagerScore";
}
/**
 * @param request
 * @return
 * 班主任score页面取值
 */
@RequestMapping("classmanager/indexDo")
@ResponseBody
public Map<String,Object> classesManagerIndexDo(HttpServletRequest request,@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
	Map<String,Object> map=new HashMap<String, Object>();
//	HttpSession session=request.getSession();
//	User loginUser=(User)session.getAttribute("loginUser");

	
	try {
		List<Integer> cidList=examService.findCidByClassManagerId(5);
	
		List<Integer> studentIdList=examService.findUidByClassIdList(cidList);
		List<Score> scoreList=examService.findScoreByStudentIdList(studentIdList);
	
		//饼图	
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {

				count1++;
			}
			if (score.getScore() > 80) {
				count2++;
			}
			else{
				count3++;
			}
		}

		List<User> userList=examService.findUserAll();
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		
//		map.put("loginUser", loginUser);
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		map.put("userList",userList);
		map.put("examList",examList);
		map.put("courseList", courseList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	

	return map;
}






/**
 * @return
 * 教学管理scoreindex
 */
@RequestMapping("teachermanager/index")
public String teacherManagerIndex() {
	System.out.println("ScoreController.teacherManagerIndex()");
	return "WEB-INF/exam/score/teachermanagerScore";
}

/**
 * @param request
 * @return
 * 教学管理score页面取值
 */
@RequestMapping("teachermanager/indexDo")
@ResponseBody
public Map<String,Object> teacherManagerIndexDo(HttpServletRequest request, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
	Map<String,Object> map=new HashMap<String, Object>();
	try {
		PageHelper.startPage(pageNum, pageSize);
		List<Score> scoreList = examService.findScoreAll();
		List<Integer> ssList=new ArrayList<Integer>();
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {
//				ssList.set(0, count1++);
				count1++;
			}
			if (score.getScore() > 80) {
//				ssList.set(1, count3++);
				count2++;
			}
			else{
//				ssList.set(2, count2++);
				count3++;
			}
		}
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		List<User> userList=examService.findUserAll();
//		map.put("ssList", ssList);
		PageInfo<Score> pageInfo = new PageInfo<Score>(scoreList);

		map.put("pageInfo", pageInfo);
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		map.put("userList", userList);
		map.put("courseList", courseList);
		map.put("examList", examList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	
	return map;
}





/**
 * @return
 * 管理员scoreindex
 */

@RequestMapping("admin/index")
public String adminIndex() {
	System.out.println("ScoreController.teacherManagerIndex()");
	return "WEB-INF/exam/score/adminScore";
}

/**
 * @param request
 * @return
 * 管理员score页面取值
 */
@RequestMapping("admin/indexDo")
@ResponseBody
public Map<String,Object> adminIndexDo(){
	Map<String,Object> map=new HashMap<String, Object>();
	try {
		
		List<Score> scoreList = examService.findScoreAll();
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {

				count1++;
			}
			if (score.getScore() > 80) {
				count2++;
			}
			else{
				count3++;
			}
		}
		
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		List<User> userList=examService.findUserAll();
		
		map.put("userList", userList);
		map.put("courseList", courseList);
		map.put("examList", examList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	
	return map;
}





/**
 * @return
 * 就业专员scoreindex
 */
@RequestMapping("emp/index")
public String empIndex() {
	System.out.println("ScoreController.teacherManagerIndex()");
	return "WEB-INF/exam/score/empScore";
}

/**
 * @param request
 * @return
 * 就业专员score页面取值
 */
@RequestMapping("emp/indexDo")
@ResponseBody
public Map<String,Object> empIndexDo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
	Map<String,Object> map=new HashMap<String, Object>();
	try {
		
		List<Score> scoreList = examService.findScoreAll();
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {

				count1++;
			}
			if (score.getScore() > 80) {
				count2++;
			}
			else{
				count3++;
			}
		}
		
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		List<User> userList=examService.findUserAll();
		
		map.put("userList", userList);
		map.put("courseList", courseList);
		map.put("examList", examList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	
	return map;
}

/**
 * @param score
 * @return
 * 编辑成绩
 */
@RequestMapping("editScore")
@ResponseBody
public Map<String,Object> edit(Score score) {
	Map<String,Object> map=new HashMap<String, Object>();
	System.out.println("ScoreController.edit()");
	System.out.println("***********************************");
	System.out.println(score.getSid());
	
	try {
		Score score2=examService.findScoreBysid(score.getSid());	
		score2.setScore(score.getScore());
		examService.editScore(score2);
		map.put("result", "ok");
	}catch(Exception e) {
		map.put("result", "no");
		e.printStackTrace();
	}
	
	return map;
}


/**
 * @param sid
 * @return
 * 删除成绩信息
 */
@RequestMapping("deleteScore")
@ResponseBody
public Map<String,Object> deleteScore(Integer sid) {
	Map<String,Object> map=new HashMap<String, Object>();
    System.out.println("ScoreController.deleteScore()");
	System.out.println("***********************************");
	System.out.println(sid);
	try {
		examService.deleScoreBySid(sid);
		map.put("result", "ok");
	}catch(Exception e) {
		map.put("result", "no");
		e.printStackTrace();
	}
	
	return map;

}


/**
 * @param score
 * @return
 * 增加成绩信息
 */
@RequestMapping("addScore")
@ResponseBody
public Map<String,Object> addScore(Score score) {
	Map<String,Object> map=new HashMap<String, Object>();
  System.out.println("ScoreController.addScore()");
  System.out.println("***************************************");
	try {
		examService.addScore(score);
		map.put("result", "ok");
	}catch(Exception e) {
		map.put("result", "no");
		e.printStackTrace();
	}
	
	return map;

}


/**
 * @param uname
 * @param cname
 * @param daterange1
 * @param daterange2
 * @return
 * 学生名，班级名，日期条件查询
 */
@RequestMapping("searchBy")
@ResponseBody
public Map<String,Object> searchBy(String uname,String cname,String daterange1,String daterange2) {
	Map<String,Object> map=new HashMap<String, Object>();
  
	try {
		List<Score> scoreList=new ArrayList<Score>();
	
//		if(!uname.equals(null)&&!uname.trim().equals("")) {
//			List<Integer> uidList=examService.findUidByLikeUname(uname);
//			List<Integer> eidList=examService.findEidByDaterange(daterange1,daterange2);
//			scoreList=examService.findScoreByEidListAndSidList(uidList,eidList);
//	}else {
		List<Integer> classIdList=examService.findCidByLikeCname(cname);
		List<Integer> uidList=examService.findUidByClassIdList(classIdList);
		List<Integer> eidList=examService.findEidByDaterange(daterange1,daterange2);
		scoreList=examService.findScoreByEidListAndSidList(uidList,eidList);
//	}
//	
			
		
		
		
		Integer count1=0;
		Integer count2=0;
		Integer count3=0;	
		for (Score score : scoreList) {
			if (score.getScore() < 60) {

				count1++;
			}
			if (score.getScore() > 80) {
				count2++;
			}
			else{
				count3++;
			}
		}
		
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		
		List<Exam> examList=examService.findExamAll();
		List<Course> courseList=examService.findCourseAll();
		List<User> userList=examService.findUserAll();
		
		map.put("userList", userList);
		map.put("courseList", courseList);
		map.put("examList", examList);
		map.put("scoreList", scoreList);
		map.put("result", "ok");
	    System.out.println("**********************************");
//	    for (Integer uid : uidList) {
//			System.out.println(uid);
//		}
//	    for (Integer uid : uidList) {
//			System.out.println(uid);
//		}
	    System.out.println(daterange1);
	    System.out.println(uname);
	    System.out.println("ok");
	    System.out.println("**********************************");
	}catch(Exception e) {
		map.put("result", "no");
		e.printStackTrace();
	}
	
	return map;

}



/**
 * @param request
 * @return
 */
@RequestMapping("test/indexDo")

public Map<String,Object> testIndex(HttpServletRequest request){
	Map<String,Object> map=new HashMap<String, Object>();
	System.out.println("ScoreController.testIndex()");
	try {
		List<Classes> classList=examService.findClassAll();
		map.put("classList", classList);
		map.put("result", "ok");
	}catch(Exception e){
		map.put("result","no");
		e.printStackTrace();
	}
	return map;
}



}
